<?php
/**
 * @auther zlq
 * @create_time 2020/5/19 15:12
 * @description
 */


namespace app\jobmanagement\service;


use think\Model;
use app\model\Product as ProductModel;
use app\model\TaskTask as TaskModel;
use app\model\ProductWork as ProductWorkModel;
use app\model\User as UserModel;
use app\model\ProductLadder as ProductLadderModel;
use app\model\ProductLadderItem as ProductLadderItemModel;

class Common extends Model
{

    /**
     * @auther zlq
     * @create_time 2020/5/19 15:21
     * @description 获取产品列表
     * @param array $param 相关参数
     * @return array|bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getProductList($param = [])
    {
        $mProd = new ProductModel();
        // 查询部门
        $dept_id = session('dept_id');
        if (isset($param['type']) && $param['type'] === 3) {
            // 这种情况需要筛序出部门有可以使用的阶梯计费的产品
            $prod = $mProd
                ->alias('a')
                ->join('product_ladder b', 'a.id = b.prod_id', 'INNER')
                ->field('a.id, a.name, a.amount, a.unit')
                ->group('a.id')
                ->where(['a.dept_id' => $dept_id, 'b.status' => 1])
                ->select();
        } else {
            $prod = $mProd
                ->field('id, name, amount, unit')
                ->where(['dept_id' => $dept_id])
                ->select();
        }
        if (empty($prod)) {
            return [];
        } else {
            $prod_unit = config('dic.prod_unit');
            foreach ($prod as $key => $item) {
                $prod[$key]['unit'] = $prod_unit[$item['unit']];
                // 查询对应的工种
                $mProdWork = new ProductWorkModel();
                $work = $mProdWork
                    ->where(['prod_id' => $item['id']])
                    ->field('id as work_id, name as work_name')
                    ->select();
                if (empty($work)) {
                    $prod[$key]['work'] = [];

                } else {
                    $prod[$key]['work'] = $work;
                }
            }
            return $prod;
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/7/28 15:52
     * @description 获取某产品的阶梯计费方式
     * @param int $prod_id 产品ID
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getProductLadder($prod_id = 0)
    {
        if (!$prod_id) {
            return [];
        }
        $mProdModel = new ProductModel();
        $prod = $mProdModel->where(['id' => $prod_id])->find();
        if (empty($prod)) {
            return [];
        }
        $prodLadderModel = new ProductLadderModel();
        $prodLadder = $prodLadderModel->where(['prod_id' => $prod_id, 'status' => 1])->select();
        if (empty($prodLadder)) {
            return [];
        }
        $data = [];
        $prodLadderItemModel = new ProductLadderItemModel();
        foreach ($prodLadder as $item) {
            $temp = $prodLadderItemModel->where(['ladder_id' => $item['id']])->select();
            if (empty($temp)) {
                continue;
            }
            $item['item'] = $temp;
            $data[] = $item;
        }
        return $data;
    }

    /**
     * @auther zlq
     * @create_time 2020/5/20 8:36
     * @description 获取一条新的任务单编号
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNewTaskCode()
    {
        $mTask = new TaskModel();
        $last_task = $mTask
            ->order('id desc')
            ->field('task_code')
            ->find();
        if (empty($last_task)) {
            $new_code = 'T'.date('YmdHis').'0001';
        } else {
            $last_code = $last_task['task_code'];
            $date = substr($last_code, 1, 8);
            if (date('YmdHis') == $date) {
                $sort = substr($last_code, 9, 3);
                $sort = (int)$sort + 1;
                $sort_str = str_pad($sort, 3 , '0', STR_PAD_LEFT);
                $new_code = 'T'.$date.$sort_str;
            } else {
                $new_code = 'T'.date('YmdHis').'0001';
            }
        }
        return $new_code;
    }


    /**
     * @introduction:根据钉钉ID查询数据库是否存在该用户
     * @param $dd_user_id
     * @return array|bool|false|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:cxh
     * @date: 2020/5/20 16:46
     */
    public function getUserInfo($dd_user_id){
        $mUser = new UserModel();
        $userInfo = $mUser
            ->where('dd_user_id',$dd_user_id)
            ->find();

        return $userInfo;
    }


    /**
     * @introduction:每次检测数据库中是否存在该用户，没有的话就新增入库
     * @param $param
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:cxh
     * @date: 2020/5/20 16:56
     */
    public function saveUserInfo($param){
        //先检查数据库中是否存在该钉钉用户
        $userInfo = $this->getUserInfo($param->userid);
        if(empty($userInfo)){
            $mUser = new UserModel();
            $data = [
                  'dd_user_id' => $param->userid,
                  'name' => $param->name,
                  'is_sys' => $param->is_sys ? 1 : 0,
                  'level' => 3,
                  'dept_id' => 0,
            ];
            $uid = $mUser->insertGetId($data);

        }
        return true;
    }


    /**
     * @auther zlq
     * @create_time 2020/6/10 8:34
     * @description 获取系统所有管理员
     * @return bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAdminList()
    {
        $UserModel = new UserModel();
        $list = $UserModel
            ->where('level', 1)
            ->where('dept_id', '>', 0)
            ->order('dept_id asc')
            ->select();
        if (empty($list)) {
            return [];
        } else {
            $plant = config('dic.plant_area');
            foreach ($list as $key => $item) {
                $list[$key]['dept_name'] = $plant[$item['dept_id']];
            }
            return $list;
        }
    }


}